<?php
// print_r($_REQUEST);
// print "<br>";
// print_r($_COOKIE);

ini_set("display_errors","0");

ob_start();

if ( preg_match("/MSIE 8.0/i",$_SERVER["HTTP_USER_AGENT"]) )
	$_REQUEST["browser"] = "IE7";
elseif ( preg_match("/MSIE 7.0/i",$_SERVER["HTTP_USER_AGENT"]) )
	$_REQUEST["browser"] = "IE7";
elseif ( preg_match("/MSIE 6.0/i",$_SERVER["HTTP_USER_AGENT"]) )
	$_REQUEST["browser"] = "IE6";
else
	$_REQUEST["browser"] = "";

require "configs/config.php";
require "configs/configSite.php";

foreach($_LIB_PHP as $val)
				require $val;

$appLog = new AppLog();
$appLog->startProcess( "Main" );

if ( ! isset( $_REQUEST['mod'] ) ) $_REQUEST['mod'] = '';

if ( $_REQUEST['mod'] != 'FUNZIONI' )
{
	$connDB = ADONewConnection($_DSN);
	//$socket = new Socket( SERVICEHOST, SERVICEPORT );

	$security = new Security();
	$security->appLog = new AppLog();
	$security->request =& $_REQUEST;
	$security->connDB =& $connDB;
	$security->func = new Func();
	$security->sqlFunc = new SqlFunc();
}

$PATH_BASE = "modules/";

$headerMessage = "";

$appLog->startProcess( "AUTH" );
if ( isset( $_REQUEST["user"] ) || $_REQUEST["mod"] == "" || $_REQUEST["do"] == "" )
{
	$security->checkAuthDB( $_REQUEST['user'], $_REQUEST['password'], $idUtente, $ruolo, $nominativo, $idAnagrafica, $voip, $operatore);
	if ( $idUtente >= 0 &&  $idUtente != '' )
	{
		//$security->checkSessionDelete();
		
		$_REQUEST['sessid'] = $security->makeSession( $sessid );
		$_SESSION['sessid'] = $_REQUEST["sessid"];
		$_SESSION['alias'] = $_REQUEST["user"];
		$_SESSION['password'] = $_REQUEST["password"];
		$_SESSION['idUtente'] = $idUtente;
		$_SESSION['operatore'] = $operatore;
		$_SESSION['nominativo'] = $nominativo;
		$_SESSION['idAnagrafica'] = $idAnagrafica;
		$_SESSION['voip'] = $voip;
		$_SESSION['idCampagna'] = '';
		$_SESSION['urlCampagna'] = '';
		
// 		$command="dir ";
// 		$output = shell_exec("$command");
// 		echo(nl2br($output)); 
// 		exit;
		//$security->insertSessionDB();
		
		$ruolo = strtoupper($ruolo);
// 		$ruolo = "USER";
		$_SESSION['ruolo'] = $ruolo;
		
		//$headerMessage = "Benvenuto";
		
		$modDefaultStr = file_get_contents("$PATH_BASE/$ruolo/modDefault.ini");
 		//print 	"$PATH_BASE/$ruolo/modDefault.ini";	
		$modDefault = split( "\n", $modDefaultStr );
		
		
		//print_r ($modDefault);
		
		//print "<<<";
		foreach( $modDefault as $val)
		{
			$key = split( "=" , $val);
			if ( $key[0] == 'mod' ) $_REQUEST[ 'mod' ] = $key[1];
			if ( $key[0] == 'do' ) $_REQUEST["do"] = $key[1];
			if ( $key[0] == 'modVoce' ) $_REQUEST["modVoce"] = $key[1];
			
			//print ">>".$_REQUEST[ 'mod' ]."<< >>".$_REQUEST["do"]."<<";
			
		}
		session_write_close();
	}
	else
	{
		$_REQUEST["mod"] = "LOGIN";
		$_REQUEST["do"] = "login";
		$_REQUEST["sessid"] = "";
	}
}
elseif ( $_REQUEST["mod"] == '' && $_REQUEST["do"] == '' )
{
	$_REQUEST["mod"] = "LOGIN";
	$_REQUEST["do"] = "login";	
}
elseif ( $_REQUEST["modVoce"] == '' && $_REQUEST["modScript"] == "endSession" )
{
	$security->closeSessionDB( $_REQUEST["sessid"] );
// 	$_SESSION['ruolo'] = '';
	$_REQUEST['modVoce'] = 'LOGIN';
	$_REQUEST['modScript'] = 'login';
	$security->error = '';
}
elseif ( $_REQUEST["mod"] != "FUNZIONI" )
{
	//$security->checkAuthDB($_SESSION['alias'], $_SESSION['password'], $_SESSION['idUtente'], $_SESSION['ruolo'], $_SESSION['idAnagrafica'])
	if ( $security->checkSessionDB( $_REQUEST['sessid'] ) )
	{
		$_SESSION["ruolo"] = '';
		$_REQUEST["mod"] = "LOGIN";
		$_REQUEST["do"] = "login";
	}
}
$appLog->endProcess( "AUTH" );


/* ************************************************************************* */
/*if ( $_REQUEST["mod"] == "utente" &&  $_REQUEST["do"] != "" && $_REQUEST["modScript"] != "login" ) 
{
	$_SESSION["ruolo"] = "USER";
}*/
$MOD = "";

if ( $_REQUEST["mod"] != 'FUNZIONI' )
{
	
	if ( isset($_SESSION["ruolo"]) )
		if ( $_SESSION["ruolo"] != '' ) 
			$MOD = $_SESSION['ruolo'];
		else
			$MOD = "LOGIN";

	if ( isset($_REQUEST["modVoce"]) )
		if ( $_REQUEST["modVoce"] != '' ) $_REQUEST["mod"] = $_REQUEST["modVoce"];

	if ( isset($_REQUEST["modScript"]) )
		if ( $_REQUEST["modScript"] != '' ) $_REQUEST["do"] = $_REQUEST["modScript"];

	if ( isset($_REQUEST['mod']) )
		if ( $_REQUEST['mod'] != '' ) $MOD.= "/".$_REQUEST["mod"];

	if ( isset($_REQUEST['do']) )
	{
		if ( $_REQUEST["do"] == "blank" && $_REQUEST["mod"] != '' ) $_REQUEST["do"] = $_REQUEST["mod"];
	}
 	//print $_SESSION['ruolo']."--------".$_REQUEST['modVoce']."------".$_REQUEST['modScript']."--------".$_REQUEST['mod']."------".$_REQUEST['do'];
}
else
{
	$MOD = $_REQUEST["mod"];
}

//print $MOD;

require $PATH_BASE.$MOD."/".$_REQUEST["do"].".php";
eval("\$module = new ".$_REQUEST["do"]."();");

/* ************************************************************************* */

$module->request =& $_REQUEST;

if ( isset($module->session) ) $module->session =& $_SESSION;

if ( isset( $connDB ) && isset( $module->connDB ) )
	$module->connDB =& $connDB;
elseif ( isset( $_DSN ) && isset( $module->connDB ) )
	$module->connDB = ADONewConnection($_DSN);

$module->func = new Func();

if ( isset( $module->analyzeError ) )
	if ( $module->analyzeError )
		$module->analyzeError = new AnalyzeError();

if ( isset( $module->analyzeArray ) )
	if ( $module->analyzeArray )
		$module->analyzeArray = new AnalyzeArray();

// if ( isset( $module->security ) )
// 	if ( $module->security )
// 		$module->security =& $this->security;

if ( isset( $connDB ) && isset( $module->connDB ) )
{
	if ( $module->connDB )
	{
		$module->funcDB = new FuncDB();
		$module->funcDB->connDB =& $connDB;
		$module->sqlFunc = new SqlFunc();
	}
}

if ( isset($module->appLog) )
{
	$module->appLog = new AppLog();
	$module->appLog->mod = $_REQUEST["mod"];
	$module->appLog->do = $_REQUEST["do"];
}

if ( isset($module->iCombo) )
{
	$module->iCombo = new iCombo();
	$module->iGrid->request =& $_REQUEST;
	$module->iGrid->session =& $_SESSION;
}

if ( isset($module->iGrid) )
{
	$module->iGrid = new iGrid();
	$module->iGrid->request =& $_REQUEST;
}

if ( isset( $module->smarty ) )
{
	$appLog->startProcess( "SMARTY" );

	$module->smarty = new Smarty();
	$module->smarty->template_dir = ROOT."/templates/";
	$module->smarty->compile_dir = ROOT."/templates_c/";
	$module->smarty->config_dir = ROOT."/configs/";
	$module->smarty->cache_dir = ROOT."/cache/";
	$module->smarty->compile_check = true;
	$module->smarty->clear_compiled_tpl();
	$module->smarty->clear_all_cache();

	$module->smarty->assign("keypress_file", $_LIB_JS["keypress"]);
	$module->smarty->assign("ajax_file", $_LIB_JS["ajax"]);
	$module->smarty->assign("title",SITE);
	$module->smarty->assign("popupMain",$popupMain);

	$module->smarty->assign('sessid', $_REQUEST['sessid']);
	
	$module->smarty->assign('anno', $_REQUEST['anno']);

	$module->smarty->assign('browser', $_REQUEST['browser']);
	
	if ( $_SESSION["visione"] != 'VISIONE' )
		$module->smarty->assign("visione", 0);
	else
		$module->smarty->assign("visione", 1);
	
	$module->smarty->caching = false;
	$module->smarty->cache_lifetime = 1;

	if ( isset( $modules->popup ) )
	{
		if ( $modules->popup )
		{
			$module->smarty->caching = false;
			$module->smarty->cache_lifetime = 0;
		}
	}
	
	
// 	print_r ($_SESSION);
	
	if ( isset( $_REQUEST[ 'posLeft' ] ) ) $module->smarty->assign("posLeft",  $_REQUEST[ 'posLeft' ]);
	if ( isset( $_REQUEST[ 'posTop' ] ) ) $module->smarty->assign("posTop",  $_REQUEST[ 'posTop' ]);
	
	$appLog->endProcess( "SMARTY" );
}
	$module->smarty->assign("usrNominativo",$_SESSION["nominativo"]);
	$module->smarty->assign("usrOperatore",$_SESSION["operatore"]);
	$module->smarty->assign("ruolo",$_SESSION["ruolo"]);
	
	/*if ( $_SESSION['alias'] != "" ) 
	{
		if ( $_SESSION["ute_id"] != "" ) $_REQUEST["ute_id"] = $_SESSION["ute_id"];
		$module->smarty->assign("ute_id",$_SESSION["ute_id"]);
		
		$security->insertSessionTime( ); 
		$security->infoUser( $arr );
		
		$module->smarty->assign("ute_nome",$_REQUEST["ute_nome"]);
		$module->smarty->assign("ute_cognome",$_REQUEST["ute_cognome"]);
		$module->smarty->assign("ute_nascita",$_REQUEST["ute_nascita"]);
		$module->smarty->assign("ute_email",$_REQUEST["ute_email"]);
		$module->smarty->assign("ute_admin",$_REQUEST["ute_admin"]);
		$module->smarty->assign("ute_profilo",$_REQUEST["ute_profilo"]);
		$module->smarty->assign("cre_messaggi",$_REQUEST["cre_messaggi"]);
		$module->smarty->assign("cre_foto",$_REQUEST["cre_foto"]);
		$module->smarty->assign("cre_video",$_REQUEST["cre_video"]);
		$module->smarty->assign("cal_numero",$_REQUEST["cal_numero"]);
		$module->smarty->assign("cal_giocatore1",$_REQUEST["cal_giocatore1"]);
		$module->smarty->assign("cal_giocatore2",$_REQUEST["cal_giocatore2"]);
		
		$module->smarty->assign("numero_amicizie",$_REQUEST["numero_amicizie"]);
		$module->smarty->assign("numero_richieste",$_REQUEST["numero_richieste"]);
		$module->smarty->assign("numero_notifiche",$_REQUEST["numero_notifiche"]);
		// contare crediti
		
		$module->smarty->assign("idUtente",$_SESSION['ute_id']);
		
		if ($_REQUEST["inviaMessaggio"] == "S")
		{
			if ( $_REQUEST["moduleActive"] == "viola" ) $security->inviaMessaggioViola();
			else if ( $_REQUEST["moduleActive"] == "rosa" ) $security->inviaMessaggioRosa();	
			else $security->inviaMessaggio();	
		}
		
		
		if ( isset($_REQUEST["messageHeaderTrue"]) && $_REQUEST["messageHeaderTrue"] != "")
			$headerMessage = $_REQUEST["messageHeaderTrue"];
		
		//messaggio che scende da sopra
		$module->smarty->assign("headerMessage", $headerMessage );
		$headerMessage = "";
	
		$module->smarty->assign("audioActive",$_REQUEST["audioActive"]);
	}*/


if ( isset( $_CLASS ) )
{
	foreach($_CLASS as $key => $val)
	{
		require_once $val;
	
		//$tmp = split("/", $val);
		//$final = $tmp[ count($tmp) - 1 ];
		//$reqClass = preg_replace("/\.php/", "", $final);

		eval("\$module->$key = new $key();");
	
		eval("\$module->".$key."->request =& \$_REQUEST;");
		
		eval("\$module->".$key."->session =& \$_SESSION;");
	
		eval("\$module->".$key."->connDB =& \$connDB;");
		
		//eval("\$module->".$key."->analyzeArray = new AnalyzeArray();");
		//eval("\$module->".$key."->funcDB =& \$module->funcDB;");
		eval("\$module->".$key."->sqlFunc = new sqlFunc();");
		eval("\$module->".$key."->appLog =& \$module->appLog;");
		eval("\$module->".$key."->func = new Func();");
		
		if ( isset( $module->usrError ) ) eval("\$module->".$key."->usrError =& \$module->usrError;");
		if ( isset( $module->sysError ) ) eval("\$module->".$key."->sysError =& \$module->sysError;");
		if ( isset( $module->usrInfo ) ) eval("\$module->".$key."->usrInfo =& \$module->usrInfo;");

		//$module->extClass[$reqClass]->smarty =& $module->smarty;	
	}
}

if ( isset( $_REQUIRE ) )
{
	foreach( $_REQUIRE as $val )
		require_once $val;
}

if ( $_REQUEST["do"] == 'login' ) $module->error_auth = $security->error;

/* Paginazione */
if ( isset($module->menu) )
{
	if ( $module->menu )
	{
		if ( ! isset($_REQUEST[ 'pageid' ]) )
			if ( $_REQUEST[ 'pageid' ] == '' ) $_REQUEST[ 'pageid' ] = 1;

		if ( isset($_REQUEST[ 'pageid' ]) && $_SESSION[ 'prevDo' ] != $_REQUEST[ 'do' ] ) $_REQUEST[ 'pageid' ] = 1;
	}
}

/* START */
$appLog->startProcess( 'START MODULE' );
if ( $retMod = $module->start() )
{
	if ( preg_match("/closeWindow/i", $retMod) ) $module->smarty->assign('closeWindow', 'window.close();');
	if ( preg_match("/refreshOpener/i", $retMod) ) $module->smarty->assign('refreshOpener', 'window.opener.document.main.submit();');
	if ( preg_match("/executeScript/i", $retMod) ) 
		$module->smarty->assign('executeScript', 1);
	else
		$module->smarty->assign('executeScript', 0);
}
$appLog->endProcess( 'START MODULE' );

if ( isset( $module->usrError ) )
{
	if ( count($module->usrError) > 0 )
	{
		$module->smarty->assign('usrError',1);
	
		$module->usrMessageDsc = "<br/>";
		foreach ($module->usrError as $key => $val )
		{
			$module->usrMessageDsc.= $val."<br/>"; 
		}
	}
	else
	{
		$module->smarty->assign('usrError',0);
	}	
}

if ( isset( $module->sysError ) )
{
	if ( count($module->sysError) > 0 )
	{
		$module->smarty->assign('sysError',1);
		$module->usrMessageDsc = "<br/>";
		foreach ($module->sysError as $key => $val )
		{
			$module->usrMessageDsc.= $val."<br/>"; 
		}
	}
	else
	{
		$module->smarty->assign('sysError',0);
	}	
}

if ( isset( $module->usrInfo ) )
{
	if ( count($module->usrInfo) > 0 )
	{
		$module->smarty->assign('usrInfo',1);
	
		$module->usrMessageDsc = "<br/>";
		foreach ($module->usrInfo as $key => $val )
		{
			$module->usrMessageDsc.= $val."<br/>"; 
		}
	}
	else
	{
		$module->smarty->assign('usrInfo',0);
	}	
}

// 	print ">>>>".$module->usrMessageDsc;
// 	print ">>>>".$module->usrMessageButtonJS;
// 	print ">>>>".$module->usrErrorFuncJS;
// 	print ">>>>".$module->usrErrorLabelJS;
// 	print ">>>>".$module->usrMessageClassJS;
	
if ( isset($module->usrMessageDsc) )
	if ( $module->usrMessageDsc != '' )
		$module->smarty->assign("usrMessageDsc", $module->usrMessageDsc);
	
if ( isset($module->usrMessageFuncJS) )
	if ( $module->usrMessageFuncJS != '' )
		$module->smarty->assign("usrMessageFuncJS", $module->usrMessageFuncJS);
	
if ( isset($module->usrMessageLabelJS) )
	if ( $module->usrMessageLabelJS != '' )
		$module->smarty->assign("usrMessageLabelJS", $module->usrMessageLabelJS);
	
if ( isset($module->usrMessageFuncJS2) )
	if ( $module->usrMessageFuncJS2 != '' )
		$module->smarty->assign("usrMessageFuncJS2", $module->usrMessageFuncJS2);
	
if ( isset($module->usrMessageLabelJS2) )
	if ( $module->usrMessageLabelJS2 != '' )
		$module->smarty->assign("usrMessageLabelJS2", $module->usrMessageLabelJS2);	

/* Paginazione */
if ( isset($module->lastPageNo) )
{
	$module->smarty->assign("pageLast", $module->lastPageNo);

	if ( $module->lastPageNo > 0 )
	{
		for($i = 1 ; $i <= $module->lastPageNo ; $i++)
			$pageTotal[] = $i;
		$module->smarty->assign("pageTotal", $pageTotal);
		$module->smarty->assign("pageid", $_REQUEST["pageid"]);
		if ( $module->recordNo )
			$module->smarty->assign("recordNo", $module->recordNo);
	}
}

if ( isset( $_REQUEST["order"] ) )
{
	$module->smarty->assign("order", $_REQUEST["order"]);
	$module->smarty->assign("orderType", $_REQUEST["orderType"]);
}

if ( isset( $module->menu ) )
{
	if ( $module->menu )
	{
		$menu = new Menu();
		
		$menu->draw( ROOT."$PATH_BASE/".$_SESSION["ruolo"] , $menu, $parentMenu, $vociMenu, $tipoUrl, $modMenu, $doMenu, $totMenu);
		
		$module->smarty->assign("subMenuView", 0);
		
		$modVoce = $_REQUEST['modVoce'];
		
		$module->smarty->assign("menu", $menu);
		$module->smarty->assign("parentMenu", $parentMenu);
		$module->smarty->assign("vociMenu", $vociMenu);
		$module->smarty->assign("tipoUrl", $tipoUrl);
		$module->smarty->assign("modMenu", $modMenu);
		$module->smarty->assign("doMenu", $doMenu);
		
		$module->smarty->assign("modVoce", $modVoce);
		$module->smarty->assign("modVocePrec", $modVoce);
		$module->smarty->assign("menuView", 1);
	}
	else
	{
		$module->smarty->assign("menuView", 0);
	}
}

if ( isset( $module->smarty ) )
{
	if ( isset($_REQUEST['resolutionClient']) )
	{
	//print $_REQUEST['resolutionClient']."<br>";
		$module->smarty->assign('resolutionClient',$_REQUEST['resolutionClient']);
		$tmp = split('x', $_REQUEST['resolutionClient']);
		$module->smarty->assign('resolutionClientX',$tmp[0]);
		$module->smarty->assign('resolutionClientY',$tmp[1]);
	}
	else
	{
		$module->smarty->assign('resolutionClient','1100x500');
		$module->smarty->assign('resolutionClientX','1100');
		$module->smarty->assign('resolutionClientY','500');
	}
}

$appLog->startProcess( 'SHOW SMARTY' );
$redirect = $module->show();

$appLog->endProcess( 'SHOW SMARTY' );


if ( $redirect )
{
	//print "<br>>>>>>>>>>>>>>>>>>redirect<<<<<<<<<<<<<<<<<<<br>";

	if ( isset( $redirect["extra"] ) ) 
		$redirect["extra"] = "&".$redirect["extra"];
	else
		$redirect["extra"] = "";
		
	if ( ! isset( $_REQUEST["sessid"] ) ) 
			$_REQUEST["sessid"] = "";
	
	//$module->appLog->logWrite("redirect: Main.php?mod=".$redirect["mod"]."&do=".$redirect["do"]."&sessid=".$_REQUEST["sessid"].$redirect["extra"], 9);
	
	header("location: Main.php?mod=".$redirect["mod"]."&do=".$redirect["do"]."&sessid=".$_REQUEST["sessid"].$redirect["extra"]);
}

if ( isset( $module->menu ) )
{
	if ( $module->menu )
	{
		$_SESSION[ 'prevMod' ] = $_REQUEST[ 'mod' ];
		$_SESSION[ 'prevDo' ] = $_REQUEST[ 'do' ];
	}
}
ob_flush();
$appLog->endProcess( "Main" );
?> 
